import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class file {
    public static String getDir(String filePath) {
        String[] paths = filePath.split("\\\\");
        String newPath = "";
        for (int i = 0; i < paths.length - 1; i++) newPath += (paths[i] + "\\");
        return newPath;
    }
    public static String[] getFile(String dirPath) {
        File f = new File(dirPath);
        return f.list(new 过滤器());
    }
    public static void main(String[] args) {
        String filePath = "C:\\Users\\22453\\Desktop\\test1.txt";
        File f = new File(filePath);
        //无论有没有 都会输出
        System.out.println(f);
        System.out.println(f.getName());
        //w文件
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String dirPath = getDir(filePath);
        for(String s:getFile(dirPath)) System.out.println(s);
        //过滤器

    }


}

class 过滤器 implements FilenameFilter {
    public boolean accept(File f, String strPath) {
        return strPath.endsWith("txt");
    }


}
